package com.zhang.controller.discount;

import com.zhang.common.util.result.R;
import com.zhang.common.util.result.RUtils;
import com.zhang.discount.dto.SkuFlashSaleSearchDTO;
import com.zhang.discount.entity.FlashSales;
import com.zhang.discount.to.FlashSaleSkuModel;
import com.zhang.discount.vo.FlashSaleSkuInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.zhang.discount.entity.FlashSaleSku;
import com.zhang.discount.service.FlashSaleSkuService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.HashMap;
import java.util.List;

/**
 * 秒杀与sku的关联表
 *
 * @author 创世神
 * @since 2024-09-28 10:03:22
 */
@RestController
@RequestMapping("/flashSaleSku")
@Api(tags = "秒杀与sku的关联表控制器")
public class FlashSaleSkuController {

    @Autowired
    private FlashSaleSkuService flashSaleSkuService;

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询", produces = "application/json")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功")})
    @GetMapping
    public R findPage(SkuFlashSaleSearchDTO dto) {
        Page<FlashSaleSku> result = flashSaleSkuService.findPage(dto);
        return RUtils.successData(result);
    }

    /**
     * 列表查询
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "列表查询", notes = "列表查询", produces = "application/json")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功")})
    @PostMapping("/findList")
    public ResponseEntity<List<FlashSaleSku>> findList(@RequestBody FlashSaleSku params) {
        List<FlashSaleSku> result = flashSaleSkuService.findList(params);
        return ResponseEntity.ok(result);
    }

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "查询", notes = "查询详情")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功")})
    @GetMapping("/{id}")
    public ResponseEntity<FlashSaleSku> findById(@PathVariable("id") Long id) {
        FlashSaleSku flashSaleSku = flashSaleSkuService.findById(id);
        return ResponseEntity.ok(flashSaleSku);
    }

    /**
     * 新增
     *
     * @param flashSaleSku
     * @return
     */
    @ApiOperation(value = "新增", notes = "新增数据")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @PostMapping
    public R insert(@RequestBody FlashSaleSku flashSaleSku) {
        flashSaleSkuService.insert(flashSaleSku);
        return RUtils.successMsg("操作成功");
    }

    /**
     * 修改
     *
     * @param flashSaleSku
     * @return
     */
    @ApiOperation(value = "修改", notes = "修改数据")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @PutMapping
    public ResponseEntity<Boolean> update(@Validated @RequestBody FlashSaleSku flashSaleSku) {
        boolean result = flashSaleSkuService.update(flashSaleSku);
        return ResponseEntity.ok(result);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除", notes = "删除数据")
    @DeleteMapping("/{id}")
    public ResponseEntity<Integer> delete(@PathVariable("id") Long id) {
        int result = flashSaleSkuService.delete(id);
        return ResponseEntity.ok(result);
    }

    /**
     *
     *
     * @return {@link R}
     */
    @GetMapping("current/sessions")
    public R getFlashSessionList() {
        List<FlashSales> flashSales = flashSaleSkuService.getCurrentFlashSaleSession();
        return RUtils.successData(flashSales);
    }

    /**
     * 查询该商品的秒杀信息
     *
     * @param skuId 商品id
     * @return {@link R}
     */
    @GetMapping("info/{skuId}")
    public R getFlashSkuBySkuId(@PathVariable String skuId) {
        FlashSaleSkuInfoVO infoVO = flashSaleSkuService.getFlashSkuBySkuId(skuId);
        return RUtils.successData(infoVO);
    }

    /**
     * 获取一个秒杀场次下的sku集合信息
     *
     * @param sessionId 场次id
     * @return {@link R}
     */
    @GetMapping("session/sku")
    public R getFlashSkuList(@RequestParam String sessionId) {
        List<FlashSaleSkuModel> sessionSku = flashSaleSkuService.getFlashSaleSkuModelBySessionId(sessionId);
        return RUtils.successData(sessionSku);
    }

    /**
     * 上架一个场次的商品
     *
     * @param sessionId
     * @return {@link R}
     */
    @PostMapping("up/sku/session/{sessionId}")
    public R upFlashSkuSession(@PathVariable String sessionId) {
        flashSaleSkuService.upFlashSkuSession(sessionId);
        return RUtils.successMsg("操作成功!");
    }
}
